//
//  FKAddBankCardVC.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/2/12.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKAddBankCardVC.h"
#import "FKBankPicker.h"

@interface FKAddBankCardVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *bankField;
@property (weak, nonatomic) IBOutlet UITextField *branchField;
@property (weak, nonatomic) IBOutlet UITextField *cardNumField;
@property (weak, nonatomic) IBOutlet UIButton *commitBtn;

@property (nonatomic, strong) NSArray *supportBanks;
@property (nonatomic, copy) NSString *bankId;

@end

@implementation FKAddBankCardVC

- (instancetype)init
{
    return [UIStoryboard storyboardWithName:@"FKAddBankCardVC" bundle:nil].instantiateInitialViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
}

- (IBAction)fieldEditingChanged:(UITextField *)sender
{
    self.commitBtn.enabled = (self.nameField.text.length > 0 &&
                              self.bankField.text.length > 0 &&
                              self.branchField.text.length > 0 &&
                              self.cardNumField.text.length > 0);
}

- (IBAction)commitBtnClicked:(UIButton *)sender
{
    if (![self.cardNumField.text fk_isRightBankCard]) {
        [SVProgressHUD showInfoWithStatus:@"请输入正确的银行卡号"];
        return;
    }
    [self addBankCard];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        if (!self.supportBanks) {
            [SVProgressHUD showInfoWithStatus:@"数据加载中，请稍后"];
            return;
        }
        FKBankPicker *picker = [[FKBankPicker alloc] init];
        picker.banks = self.supportBanks;
        [picker show];
        fkWeakSelf(self);
        picker.selectedBankBlock = ^(NSDictionary * _Nonnull dict) {
            weakself.bankField.text = dict[@"name"];
            weakself.bankId = dict[@"id"];
            [weakself fieldEditingChanged:weakself.bankField];
        };
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *finalText = [textField.text stringByAppendingString:string];
    if ([textField isEqual:self.cardNumField]) {
        return [finalText fk_isPureInt] && finalText.length <= 19;
    }
    return true;
}

- (void)loadData
{
    [SVProgressHUD show];
    [FKHttpTool postWithSuffix:@"m=Home&c=Bank&a=supportBank" param:nil success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD dismiss];
            self.supportBanks = result.data[@"list"];
        }
    } failure:nil];
}

- (void)addBankCard
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"opening_name"] = self.nameField.text;
    param[@"bank_type_id"] = self.bankId;
    param[@"opening_bank"] = self.branchField.text;
    param[@"bank_number"] = self.cardNumField.text;
    [FKHttpTool postWithSuffix:@"m=Home&c=Bank&a=addBankCard" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD showInfoWithStatus:@"添加银行卡成功"];
            if (self.addBankCardSuccess) {
                self.addBankCardSuccess();
            }
            fkPopVC;
        }
    } failure:nil];
}

@end
